<?php

/**
 * This is the model class for table "user_personal_link_statistic".
 *
 * The followings are the available columns in table 'user_personal_link_statistic':
 * @property string $id
 * @property string $user_id
 * @property string $date
 *
 * The followings are the available model relations:
 * @property User $user
 */
class UserPLStatistic extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return UserPersonalLinkStatistic the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user_pl_statistic';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id, user_id', 'required'),
			array('id, user_id', 'length', 'max'=>10),
			array('date', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, user_id, date', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user' => array(self::BELONGS_TO, 'User', 'user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'user_id' => 'User',
			'date' => 'Date',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('user_id',$this->user_id,true);
		$criteria->compare('date',$this->date,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	
	/**
	 * save data when you go to the personal link
	 * @param int $user_id
	 */
	public static function saveStatistic($user_id, $type) {
		$model = new UserPLStatistic;
		$model->user_id = $user_id;
		$model->date = date('Y-m-d');
		$model->type = $type;
		$model->save(false);
	}
	
	/**
	 * @param int $user_id
	 * @param array $period[start] $period[end]
	 */
	public static function getStatictis($user_id, $start = false, $end = false) {
		$criteria = new CDbCriteria;
		$criteria->condition = '`user_id`='. (int)$user_id;
		if($start || $end)
			$criteria->addBetweenCondition('date', $start, $end);
		
		return UserPLStatistic::model()->count($criteria);		
	}
}